<?php if (!defined('FIT_PATH')) exit('No direct script access allowed');
/**
 * @copyright 2009 - 2011 Bincent.com. All Rights Reserved.
 * @license http://www.apache.org/licenses/LICENSE-2.0
 * @package FitPHP
 * @subpackage core
 * 
 * @author Hongbin Hsu <hongbin.hsu@gmail.com> Initial.
 * @version $Id: core/router.php 1.0 2009-07-18 13:34 hongbin.hsu $
 */

// ------------------------------------------------------------------------

class Router
{
	private static $module = '';			//模块
	private static $controller = 'main';	//文件、类
	private static $method = 'index';		//方法
	private static $param = array();		//参数
	
	//----------------------------------------------------------
	/**
	 * 解析URL路径取得控制器与方法名
	 *
	 * @param array  $modules
	 * @param string $controller
	 * @param string $method
	 * @return void
	 */
	public static function dispatch($modules = null, $controller = null, $method = null)
	{
		$request_url = str_replace($_SERVER['SCRIPT_NAME'],'',$_SERVER['REQUEST_URI']);
		$splits = explode('/', trim($request_url,'/'));
		
		if (!empty($modules)) {
			if (isset($modules[$splits[0]])) {
				$module = addslashes(array_shift($splits));
				$directory = $modules[$module];
			} else {
				$directory = $modules['default'];
			}
			self::$module = trim($directory, '/');
		}
		
		if (!empty($controller)) { self::$controller = $controller; }
		if (!empty($method)) { self::$method = $method; }

		$controller	= addslashes(array_shift($splits));
		$method		= addslashes(array_shift($splits));
		self::$controller	= empty($controller) ? self::$controller : $controller;
		self::$method		= empty($method) ?  self::$method : $method;
		self::$param		= $splits;
	}
	
	//----------------------------------------------------------
	public static function get_module()
	{
		return self::$module;
	}
	
	//----------------------------------------------------------
	public static function get_controller()
	{
		return self::$controller;
	}
	
	//----------------------------------------------------------
	public static function get_method()
	{
		return self::$method;
	}
	
	//----------------------------------------------------------
	public static function get_param()
	{
		return self::$param;
	}
	
	//----------------------------------------------------------
	private static function strip($param)
	{
		if (is_array($param)) {
			foreach ($param as $key => $val) {
				$param[$key] = self::strip($val);
			}
		} else {
			$param = stripslashes($param);
		}
		return $param;
	}
}